package com.fc.mall.controller;

import com.fc.mall.base.object.Result;
import com.fc.mall.dto.CommonResult;
import com.fc.mall.dto.UmsAgentApplyExcel;
import com.fc.mall.dto.UmsAgentParam;
import com.fc.mall.exception.InternalServerErrorException;
import com.fc.mall.model.UmsAgentApply;
import com.fc.mall.service.UmsAgentService;
import com.fc.mall.util.DateTimeHelper;
import com.fc.mall.util.Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import ma.glasnost.orika.MapperFacade;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Sunny on 2019/7/17.
 */
@RestController
@Api(tags = "UmsAgentController" , description="代理商申请")
@RequestMapping("/agent")
public class UmsAgentController {

    @Autowired
    private UmsAgentService agentService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("list")
    public CommonResult list(
            UmsAgentParam agentParam,
            @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam (defaultValue = "10") Integer pageSize)
    {
        List<UmsAgentApply> agentApplyList = agentService.list(agentParam, pageNum, pageSize);
        return new CommonResult().pageSuccess(agentApplyList);
    }

    @PutMapping()
    public Result approvalAgent(@RequestBody UmsAgentApply agentApply)
    {
        agentService.approveAgent(agentApply);
        return Result.success();
    }

    @ApiOperation("代理商不同状态数量查询")
    @GetMapping("count/status")
    public Result countStatus(){
        return Result.success(agentService.countStatus());
    }

    @ApiOperation("代理商申请导出")
    @GetMapping("exportxls")
    public void exportXls(UmsAgentParam agentParam, HttpServletResponse response){
        List<UmsAgentApply> agentApplyList = agentService.list(agentParam,1,1000);
        List<UmsAgentApplyExcel> agentApplyExcelList = agentApplyList.stream().map(umsAgentApply -> {
            UmsAgentApplyExcel agentApplyExcel = mapperFacade.map(umsAgentApply, UmsAgentApplyExcel.class);
            agentApplyExcel.setNickname(Utils.urlDecoderTransfer(umsAgentApply.getNickname()));
            agentApplyExcel.setApplyDate(DateTimeHelper.getDateFromEpochTimeInSecond(umsAgentApply.getApplyDate()));
            if (!StringUtils.isEmpty(umsAgentApply.getApproveDate())){
                agentApplyExcel.setApproveDate(DateTimeHelper.getDateFromEpochTimeInSecond(umsAgentApply.getApproveDate()));
            }
            agentApplyExcel.setId(umsAgentApply.getId());
            agentApplyExcel.setPhone(umsAgentApply.getPhone());
            agentApplyExcel.setReason(umsAgentApply.getReason());
            agentApplyExcel.setStatus(umsAgentApply.getStatus());
            return agentApplyExcel;
        }).collect(Collectors.toList());
        response.setHeader("Content-disposition", "attachment; filename=agentApply.xls");
        response.setHeader("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        try {
            ExcelExportUtil.exportExcel(new ExportParams("代理商申请列表", "生成人:系统", "代理商申请信息"),
                    UmsAgentApplyExcel.class, agentApplyExcelList).write(response.getOutputStream());
        } catch (IOException e) {
            throw new InternalServerErrorException("生成报表失败");
        }
    }
}
